﻿// <copyright file="DiscountSelection.xaml.cs" company="NorthCoast NERO">
//     NorthCoast NERO. All rights reserved.
// </copyright>
// <author>Eric Kociecki</author>
namespace NcnPlayerCheckIn.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for DiscountSelection.xaml.  DiscountSelection is meant to be used as a small popup that allows selection of multiple discounts.
    /// </summary>
    public partial class DiscountSelection : UserControl
    {
        /// <summary>
        /// Collection of available discounts to be displayed by this control.
        /// </summary>
        private Collection<DiscountType> availableDiscounts = new Collection<DiscountType>(new List<DiscountType>());

        /// <summary>
        /// Collection of currently selected discounts.
        /// </summary>
        private Collection<DiscountType> selectedDiscounts = new Collection<DiscountType>(new List<DiscountType>());

        /// <summary>
        /// Collection of buttons for each available discount.  The corresponding DiscountType object for each button should be store in the Tag property of the button.
        /// </summary>
        private Collection<ToggleButton> buttons = new Collection<ToggleButton>(new List<ToggleButton>());

        /// <summary>
        /// Initializes a new instance of the DiscountSelection class.
        /// </summary>
        public DiscountSelection()
        {
            this.InitializeComponent();

            ToggleButton newDiscountButton;
            TextBox newButtonLabel;

            this.availableDiscounts = DiscountTypes.AllTypes;

            foreach (DiscountType discountType in this.availableDiscounts)
            {
                newDiscountButton = new ToggleButton();
                newDiscountButton.Width = 110;
                newDiscountButton.Height = 50;
                newDiscountButton.Padding = new Thickness(0);
                newDiscountButton.Checked += new RoutedEventHandler(this.Button_Checked);
                newDiscountButton.Unchecked += new RoutedEventHandler(this.Button_Unchecked);
                newDiscountButton.Tag = discountType;

                newButtonLabel = new TextBox();
                newButtonLabel.Text = discountType.ToString();
                newButtonLabel.Background = Brushes.Transparent;
                newButtonLabel.TextWrapping = TextWrapping.Wrap;
                newButtonLabel.TextAlignment = TextAlignment.Center;
                newButtonLabel.BorderThickness = new Thickness(0);
                newButtonLabel.Height = double.NaN;
                newButtonLabel.Width = double.NaN;
                newButtonLabel.Margin = new Thickness(0);
                newButtonLabel.IsHitTestVisible = false;
                newDiscountButton.Content = newButtonLabel;

                this.mainPanel.Children.Add(newDiscountButton);
                this.buttons.Add(newDiscountButton);
            }
        }

        /// <summary>
        /// Gets or sets an object reference back to the managing control of this DiscountSelection object.
        /// </summary>
        public PlayerDisplay ManagerControl { get; set; }

        /// <summary>
        /// Gets or sets the discounts that have been selected by the user.
        /// </summary>
        public Collection<DiscountType> SelectedDiscounts
        {
            get
            {
                Collection<DiscountType> selected = new Collection<DiscountType>(new List<DiscountType>());

                DiscountType bestNonStackingDiscount = null;
                DiscountType currentDiscount = null;

                foreach (ToggleButton tb in this.buttons)
                {
                    if ((bool)tb.IsChecked)
                    {
                        currentDiscount = (DiscountType)tb.Tag;
                        if (currentDiscount.IsStackable)
                        {
                            selected.Add((DiscountType)tb.Tag);
                        }
                        else
                        {
                            if ((bestNonStackingDiscount == null) || (currentDiscount.Value > bestNonStackingDiscount.Value))
                            {
                                bestNonStackingDiscount = currentDiscount;
                            }
                        }
                    }
                }

                if (bestNonStackingDiscount != null)
                {
                    selected.Add(bestNonStackingDiscount);
                }

                return selected;
            }

            set
            {
                this.selectedDiscounts = value;

                foreach (DiscountType discount in this.selectedDiscounts)
                {
                    foreach (ToggleButton tb in this.buttons)
                    {
                        if (discount == (DiscountType)tb.Tag)
                        {
                            tb.IsChecked = true;
                            break; // micro-optimization, feel free to remove
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Event handler for each discount button's Checked event.  This adds the associated DiscountType to the collection of currently selected discounts.
        /// </summary>
        /// <param name="sender">The control the fired the event.</param>
        /// <param name="e">EventArgs related to the event.</param>
        private void Button_Checked(object sender, RoutedEventArgs e)
        {
            DiscountType clickedDiscount = (DiscountType)((ToggleButton)sender).Tag;

            if (!this.selectedDiscounts.Contains(clickedDiscount))
            {
                this.selectedDiscounts.Add(clickedDiscount);
                this.ManagerControl.NotifyDiscountPropertiesChanged();
            }
        }

        /// <summary>
        /// Event handler for each discount button's Unchecked event.  This removes the associated DiscountType from the collection of currently selected discounts.
        /// </summary>
        /// <param name="sender">The control the fired the event.</param>
        /// <param name="e">EventArgs related to the event.</param>
        private void Button_Unchecked(object sender, RoutedEventArgs e)
        {
            DiscountType clickedDiscount = (DiscountType)((ToggleButton)sender).Tag;

            this.selectedDiscounts.Remove(clickedDiscount);
            this.ManagerControl.NotifyDiscountPropertiesChanged();
        }
    }
}
